package com.powernode.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.powernode.domain.Area;
import com.powernode.model.Result;
import com.powernode.service.AreaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @create: 2024/7/19
 * @author: gyhe10
 * @description: 地区管理控制层
 **/

@Api(tags = "地区接口管理")
@RestController
@RequestMapping("admin/area")
public class AreaController {

    @Resource
    private AreaService areaService;

    /**
     * 查询全国地区列表
     * @return
     */
    @ApiOperation("查询全国地区列表")
    @GetMapping("list")
    @PreAuthorize("hasAuthority('admin:area:list')")
    public Result<List<Area>> loadAllAreaList() {
        // 提供业务层方法，将结果存入redis
        List<Area> areaList = areaService.queryAllAreaList();
        return Result.success(areaList);
    }

    /**
     * 根据pid查询子节点集合
     * @param pid
     * @return
     */
    @ApiOperation("根据pid查询子节点集合")
    @GetMapping("listByPid")
    @PreAuthorize("hasAuthority('admin:area:list')")
    public Result<List<Area>> loadAreaListByPid(@RequestParam Long pid) {
        List<Area> areaList = areaService.list(new LambdaQueryWrapper<Area>()
                .eq(Area::getParentId, pid));
        return Result.success(areaList);
    }

    /////////////// 微信小程序接口 ///////////////

    /**
     * 微信小程序中根据pid查询子节点集合
     * @param pid 父节点id
     * @return
     */
    @ApiOperation("根据pid查询子节点集合")
    @GetMapping("mall/listByPid")
    public Result<List<Area>> loadAreaListByParentId(@RequestParam Long pid) {
        List<Area> areaList = areaService.list(new LambdaQueryWrapper<Area>()
                .eq(Area::getParentId, pid));
        return Result.success(areaList);
    }


}
